package org.watchrecords.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class EnumHelper {
    @SuppressWarnings("unchecked")
    public static <T extends Enum<T>> SortedMap<String, T> getEnumMap(Class<T> klass) {
        SortedMap<String, T> map = new TreeMap<String, T>();
        T[] values;
        
        try {
            values = (T[]) klass.getMethod("values").invoke(null);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        
        for (T value: values) {
            map.put(value.toString(), value);
        }

        return map;
    }
    
    @SuppressWarnings("unchecked")
    public static <T extends Enum<T>> List<T> getEnumList(Class<T> klass) {
        List<T> values;
        
        try {
            values = new ArrayList<T>(Arrays.asList((T[]) klass.getMethod("values").invoke(null)));
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        
        Collections.sort(values, new ToStringComparator());
        return values;
    }
}
